//
//  HHDetailViewController.m
//  JSGridViewDemo
//
//  Created by Jey on 12-5-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "HHDetailViewController.h"
#import "UICustomImageView.h"
#define  kOneCellImageWidth 152//图片宽度
#define  kOnceLoadingCount 20

@interface HHDetailViewController () <JSGridViewDataSource, JSGridViewDelegate> {
    NSArray *_images;
    NSArray *_images2;
//    NSMutableArray *_images3;
}
@end

@implementation HHDetailViewController
//@synthesize detailItem = _detailItem;
@synthesize gridView = _gridView;

- (void)dealloc
{
//    [_detailItem release];
    _gridView.dataSource = nil;
    _gridView.delegate = nil;
    [_gridView release];
    [_leftArray release];
    [_middleArray release];
//    [_rightArray release];
    [_images release];
    [_images2 release];
//    [_images3 release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"shu xiang", @"Detail");
    }
    return self;
}

#pragma mark - 
- (void)viewDidLoad {
    [super viewDidLoad];
    _leftArray = [[NSMutableArray alloc] init];
    _middleArray = [[NSMutableArray alloc] init];
//    _rightArray = [[NSMutableArray alloc] init];
    _loadCount = 0;
    _isLoading = NO;
//    _images = [[NSArray alloc] initWithObjects:
//               [UIImage imageNamed:@"0.jpeg"],
//               [UIImage imageNamed:@"1.jpeg"], 
//               [UIImage imageNamed:@"2.jpeg"], 
//               [UIImage imageNamed:@"3.jpeg"], 
//               [UIImage imageNamed:@"4.jpeg"], 
//               [UIImage imageNamed:@"5.jpeg"], 
//               [UIImage imageNamed:@"6.jpeg"], 
//               [UIImage imageNamed:@"7.jpeg"], 
//               [UIImage imageNamed:@"8.jpeg"], 
//               [UIImage imageNamed:@"9.jpeg"], 
//               [UIImage imageNamed:@"10.jpeg"], nil];
    
    _images2=[[NSArray alloc] initWithObjects:@"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd69d3a525dhuodong1.png",
              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd69deac1f2huodong2.png",
              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd69e7807b1huodong3.png",
              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd69f04479ehuodong4.png",
              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd69ffef70fhuodong5.png",
              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd6a0991dfchuodong6.png",
              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd6a13b0e08huodong7.png",
              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd6a13b0e08huodong7.png",
              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd6a13b0e08huodong7.png",
              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd6a13b0e08huodong7.png",nil];
    
    _images=[[NSArray alloc] initWithObjects:@"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd69d3a525dhuodong1.png",
              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd69deac1f2huodong2.png",
              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd69e7807b1huodong3.png",
              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd69f04479ehuodong4.png",
              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd69ffef70fhuodong5.png",
              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd6a0991dfchuodong6.png",
              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd6a13b0e08huodong7.png",
              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd6a13b0e08huodong7.png",
              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd6a13b0e08huodong7.png",
              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd6a13b0e08huodong7.png",nil];
    
    
//    _images3=[NSMutableArray arrayWithObjects:@"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd69d3a525dhuodong1.png",
//              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd69deac1f2huodong2.png",
//              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd69e7807b1huodong3.png",
//              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd69f04479ehuodong4.png",
//              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd69ffef70fhuodong5.png",
//              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd6a0991dfchuodong6.png",
//              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd6a13b0e08huodong7.png",
//              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd6a13b0e08huodong7.png",
//              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd6a13b0e08huodong7.png",
//              @"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd6a13b0e08huodong7.png",nil];
//    for(int i=0;i<_images3.count;i++){
//        NSLog(@"tupian%@",[_images3 objectAtIndex:i]);
//    }
    [self addTableViewData];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    _leftArray = nil;
    _middleArray = nil;
//    _rightArray = nil;
    _images = nil;
    _images2=nil;
//    _images3=nil;
}

#pragma mark - Helper
- (NSArray *)arrayValueByTableKey:(NSInteger)columnIndex {
    NSArray *array = [NSArray array];
    if (columnIndex==0) {
        array = _leftArray;
    } else if (columnIndex==1) {
        array = _middleArray;
    }
//    else if (columnIndex==2) {
//        array = _rightArray;
//    }
    return array;
}

- (void)addTableViewData {
    NSLog(@"add");
    _loadCount += kOnceLoadingCount;
    for (int i=0; i<_loadCount; i++) {
        float height = (float)(arc4random()%120);
        if (height<100) height += 100;
        double mininum = MIN(_lefeHeight, MIN(_middleHeight, _rightHeight));
        // data info
        NSDictionary *info = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%f", height]
                                                         forKey:@"usingHeight"];
        if (_lefeHeight == mininum) {
            _lefeHeight += height;
            [_leftArray addObject:info];
        } else if (_middleHeight == mininum) {
            _middleHeight += height;
            [_middleArray addObject:info];
        }
        else if (_rightHeight == mininum) {
            _rightHeight += height;
//            [_rightArray addObject:info];
        }
    }
    [_gridView reloadData];
}

#pragma mark - JSGridView
- (JSGridViewConstSize)constSizeForGeidView:(JSGridView *)gridView {
    return JSGridViewConstSizeWidth;
}

- (CGFloat)gridView:(JSGridView *)gridView heightForCellAtRow:(NSInteger)row column:(NSInteger)column {
    id obj = [[self arrayValueByTableKey:column] objectAtIndex:row];
    return [[obj objectForKey:@"usingHeight"] intValue]+5;
}

- (CGFloat)gridView:(JSGridView *)gridView widthForCellAtColumnIndex:(NSInteger)column {
    return kOneCellImageWidth+5;
}

- (NSInteger)numberOfRowsInGridView:(JSGridView *)gridView forConstColumnWithIndex:(NSInteger)column {
    return [[self arrayValueByTableKey:column] count];
}

- (NSInteger)numberOfConstColumnsInGridView:(JSGridView *)gridView {
    return 2;
}

- (void)gridView:(JSGridView *)gridView scrolledToEdge:(JSGridViewEdge)edge {
    if (edge == JSGridViewEdgeBottom) {
//        _isLoading = YES;
        [self addTableViewData];
    }
}

- (JSGridViewCell *)gridView:(JSGridView *)gridView viewForRow:(NSInteger)row column:(NSInteger)column {


    NSArray *array = [self arrayValueByTableKey:column];
    NSString *identifier = @"testCell";
   JSGridViewCell *cell = [gridView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[JSGridViewCell alloc] initWithReuseIdentifier:identifier] autorelease];
        UICustomImageView *imageView = [[UICustomImageView alloc] initWithFrame:CGRectZero];
        imageView.tag = 50;
        //        [imageView setDelegate:self];
        //        imageView.backgroundColor = HHColor(110.0, 110.0, 110.0, 0.4);
        [cell addSubview:imageView];
        [imageView release];
        cell.delegate = self;
    }
    cell.row = row;
    cell.column = column;
    NSDictionary *oneDic = [array objectAtIndex:row];
    UICustomImageView *imageView = (UICustomImageView *)[cell viewWithTag:50];
    float height = [[oneDic objectForKey:@"usingHeight"] floatValue];
    imageView.frame = CGRectMake(5, 5, kOneCellImageWidth, height);
    

//    [imageView setImageURL:@"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd6a13b0e08huodong7.png"];
//    if(column==0){
//        NSLog(@"hang%d",row);
//    }
//
    if(column==0){
    int i = (row*column+row+column)%10;
        NSLog(@"diyi%d",i);
              [imageView setImageURL:[_images objectAtIndex:i]];
    }
    if(column==1){
        int i = (row*column)%10;
        NSLog(@"dier%d",i);
//        [imageView setImage:nil];
        [imageView setImageURL:[_images2 objectAtIndex:i]];
    }
    
    
    
    
//    if(column==1){
//        int i = (row*column)%[_images3 count];
//        NSLog(@"dier%d",i);
//       [imageView setImage:nil];
//    [imageView setImageURL:@"http://songzu.dns23.49host.com/takeout/upload/coupon/50cd6a13b0e08huodong7.png"];
////        imageView.image = [_images2 objectAtIndex:i];
//    }
    
    return cell;
}

- (void)gridViewCellWasTouched:(JSGridViewCell *)gridViewCell {
    NSLog(@"row : %d, column : %d", gridViewCell.row, gridViewCell.column);
}
@end
